package top.btcmarket.auth.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import top.btcmarket.auth.entity.User;
import top.btcmarket.auth.service.UserService;

@Controller
@RequestMapping(value = "/test")
public class TestRestController {
  
    @Autowired
    UserService userService;  //Service which will do all data retrieval/manipulation work
  
      
    //-------------------Retrieve All Users--------------------------------------------------------
    // use leo
    // Spring Security默认的角色前缀是”ROLE_”,使用hasRole方法时已经默认加上了，因此我们在数据库里面的用户角色应该是“ROLE_user”，在user前面加上”ROLE_”前缀
    // 因此 leo 需要有一个 角色的名称 为  ROLE_admin , 才能访问该接口
    
//    @PreAuthorize("hasRole('admin')")
    @RequestMapping(value = "/user/", method = RequestMethod.GET)
    @ResponseBody
    public List<User> listAllUsers() {
        List<User> users = userService.findAll();
        if(users.isEmpty()){
            return null;
        }
        return users;
    }
    
    //use aden  只需要token 就可以访问
    
    @RequestMapping(value = "/userNoRole/", method = RequestMethod.GET)
    @ResponseBody
    public List<User> listAllUsersNoRole() {
        List<User> users = userService.findAll();
        if(users.isEmpty()){
            return null;
        }
        return users;
    }
    
    
    @RequestMapping(value = "/userPath/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<User> testUrlPath(HttpServletRequest request,@PathVariable("id") String id) {
    	
    	
    	 
         User user = userService.findById(Integer.parseInt(id));
         if (user == null) {
             System.out.println("User with id " + id + " not found");
             return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
         }
         return new ResponseEntity<User>(user, HttpStatus.OK);
    }
    
    
  
  
  
    //-------------------Retrieve Single User--------------------------------------------------------
    // 查询指定的用户， 通过 url 来控制 
    
    
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})
    public ResponseEntity<User> getUser(@PathVariable("id") String id) {
        System.out.println("Fetching User with id " + id);
        User user = userService.findById(Integer.parseInt(id));
        if (user == null) {
            System.out.println("User with id " + id + " not found");
            return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }
  
      
  
  
}